// 反转链表2

package Leetcode;

// import java.util.List;

class solution_092 {
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode slow = new ListNode();
        ListNode fast = new ListNode();
        ListNode pre = new ListNode();
        pre.next = head;
        fast.next = head;
        for (int i = 0; i < left - 1; i++)
            pre = pre.next;
        for (int i = 0; i < right; i++)
            fast = fast.next;
        slow = pre.next;
        // System.out.println(slow.val);
        // System.out.println(fast.val);
        while (!slow.equals(fast)) {
            ListNode sn = slow.next;
            pre.next = sn;
            slow.next = fast.next;
            fast.next = slow;
            slow = sn;
            // for (ListNode l = head; l != null; l = l.next)
            //     System.out.print(l.val + ",");
            // System.out.println();
        }
        return left == 1 ? pre.next : head;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(3);
        // ListNode node1 = new ListNode(2);
        // ListNode node2 = new ListNode(3);
        // ListNode node3 = new ListNode(4);
        ListNode node4 = new ListNode(5);
        head.next = node4;
        // node1.next = node2;
        // node2.next = node3;
        // node3.next = node4;
        solution_092 s = new solution_092();
        s.reverseBetween(head, 1, 2);
    }
}

public class Solution092 {

}
